package com.example.demojdbcmysql.controller;

import com.example.demojdbcmysql.dao.AuthUserEntityMapper;
import com.example.demojdbcmysql.dao.AuthUserLogEntityMapper;
import com.example.demojdbcmysql.entity.AuthUserEntityExample;
import com.example.demojdbcmysql.entity.AuthUserLogEntity;
import com.example.demojdbcmysql.entity.AuthUserLogEntityExample;
import com.example.demojdbcmysql.entity.AuthUserLogEntityExample.Criteria;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Author: kai.yang
 * @Date: 2019-10-25 12:58
 */
@RestController
@RequestMapping("/")
public class TestController {

    @Autowired
    AuthUserLogEntityMapper authUserLogEntityMapper;

    @Autowired
    AuthUserEntityMapper authUserEntityMapper;


    /**
     * 测试根据时间新增日志数据
     */
    @RequestMapping("/add")
    public String test(@RequestBody AuthUserLogEntity authUserLogEntity) {
        authUserLogEntityMapper.insertSelective(authUserLogEntity);
        return "ok";
    }

    /**
     * 测试没有时间参数时的查询
     */
    @RequestMapping("/query")
    public Object query(@RequestParam(name = "account") String account) {
        AuthUserLogEntityExample authUserLogEntity = new AuthUserLogEntityExample();
        authUserLogEntity.createCriteria().andAccountEqualTo(account);
        return authUserLogEntityMapper.selectByExample(authUserLogEntity);
    }

    /**
     * 测试添加时间参数的时间查询
     */
    @RequestMapping("/query2")
    public Object query2(@RequestParam(name = "account") String account, @RequestParam(name = "start") String start
        , @RequestParam(name = "end") String end, @RequestParam(name = "ip") String ip)
        throws ParseException {
        AuthUserLogEntityExample authUserLogEntity = new AuthUserLogEntityExample();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = simpleDateFormat.parse(start);
        Date endTime = simpleDateFormat.parse(end);
        Criteria criteria = authUserLogEntity.createCriteria();
        criteria.andAccountEqualTo(account).andInvokeTimeBetween(startTime, endTime);
        if (StringUtils.isNotEmpty(ip)) {
            criteria.andUserIpEqualTo(ip);
        }

        return authUserLogEntityMapper.selectByExample(authUserLogEntity);
    }


}
